<?php


namespace app\common\model;


use think\Model;
use think\model\concern\SoftDelete;

/**
 * 有关时间的模型
 * Class TimeModel
 * @package app\common\model
 */
class TimeModel extends Model
{
    protected static $add_rule = [];
    protected static $edit_rule = [];
    protected static $add_message = [];
    protected static $edit_message = [];

    public static function init()
    {
        return new static();
    }

    /**
     * 自动时间戳类型
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 添加时间
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 软删除
     */
    use SoftDelete;

    protected $deleteTime = false;

    /**
     * @title 缓存查询某个值
     * @param $value_name
     * @param $key
     * @param null $key_name
     * @param int $seconds
     * @return mixed
     * @author FashionJune
     */
    public function getValueByKey($value_name, $key, $seconds = 60, $key_name = null)
    {
        $key_name ??= $this->getPk();
        if ($seconds > 0) {
            return $this->where($key_name, '=', $key)->cache($seconds)->value($value_name);
        } else {
            return $this->where($key_name, '=', $key)->value($value_name);
        }
    }

}